package day_2022_12_to_3.year2023;

/**
 * @author haomin
 * @date 2023/01/07 22:41
 **/
public class Day1_7_Num1658 {
    class Solution {
        public int minOperations(int[] nums, int x) {
            int n = nums.length;
            int sum = 0;
            for(int i : nums) sum += i;
            if(sum < x) return -1;
            if(sum == x) return n;
            int ans = n;
            for(int i = 0, j = 0; i < n; ++i) {
                for(;sum > x && j < n; ++j) sum -= nums[j];
                if(sum == x) ans = Math.min(ans, n - j + i);
                sum += nums[i];
            }
            return ans == n ? -1 : ans;
        }
    }
}